package com.aitao.util;

import com.aitao.enums.LineEnding;
import com.aitao.exception.CmdRunException;
import com.aitao.util.cmd.Cmd;
import com.aitao.util.cmd.CmdRunner;
import com.aitao.util.cmd.DefaultCmdRunner;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 命令行工具类
 *
 * @Author : tao.ai
 * @Create : 2021/09/29 12:50
 */
public class CmdUtils {
    private static final CmdRunner cr = new DefaultCmdRunner();
    private static String cmdStr;

    public static Cmd call(Path workDir, String... command) {
        try {
            cmdStr = String.join(LineEnding.SPACE.toString(), command);
            return cr.run(workDir, command);
        } catch (IOException e) {
            throw new CmdRunException(Assert.mark(message("call(Path workDir, String... command)"), new Throwable()));
        }
    }

    public static Cmd call(Path workDir, Collection<String> command) {
        try {
            cmdStr = String.join(LineEnding.SPACE.toString(), command);
            ;
            return cr.run(workDir, new ArrayList<>(command));
        } catch (IOException e) {
            throw new CmdRunException(Assert.mark(message("call(Path workDir, Collection<String> command)"), new Throwable()));
        }
    }

    public static Cmd call(String command) {
        try {
            cmdStr = command;
            return cr.run(command);
        } catch (IOException e) {
            throw new CmdRunException(Assert.mark(message("call(String command)"), new Throwable()));
        }
    }

    public static Cmd call(Collection<String> command) {
        try {
            cmdStr = String.join(LineEnding.SPACE.toString(), command);
            return cr.run(new ArrayList<>(command));
        } catch (IOException e) {
            throw new CmdRunException(Assert.mark(message("call(Collection<String> command)"), new Throwable()));
        }
    }

    public static Cmd call(CharSequence... command) {
        cmdStr = String.join(" ", command);
        try {
            return cr.run(null, command);
        } catch (IOException e) {
            throw new CmdRunException(Assert.mark(message("call(CharSequence... command)"), new Throwable()));
        }
    }

    private static String message(String methodName) {
        return String.format("%s#%s execute \"%s\" command error", "CmdUtils", methodName, cmdStr);
    }

    public static void main(String[] args) {
        List<String> commend = new ArrayList<>();
        //commend.add("cmd");
        //commend.add("/c");
        //commend.add("git");
        //commend.add("rev-parse");
        //commend.add("--short");
        //commend.add("HEAD");
        //commend.add("ping");
        //commend.add("www.baidu.com");
        commend.add("java");
        commend.add("-version");
        //commend.add("proguard");
        //commend.add("@C:\\Users\\10034\\Desktop\\Learn\\springboot-utils\\src\\main\\resources\\proguard.txt");
        CmdUtils.call(commend).console();
    }
}
